﻿using System;
using System.Drawing;
using System.Windows.Forms;
using SerialDemo;

namespace WindowsFormsApplication1
{
    // Skilgreini delegate (s.b.r þátt a)
    public delegate bool ShoppingItemClicked(Item item);

    public partial class DemoForm : Form
    {
        // Bý til tilvik af delegate
        public ShoppingItemClicked ShoppingItemClicked = delegate { return false; };

        private ShoppingList shoppingList;

        public ShoppingList ShoppingList { 
            get
            {
                return this.shoppingList;
            }
            set
            {
                this.shoppingList = value;
                this.dgv.DataSource = this.ShoppingList.Items;
                this.dgv.Refresh();
            }
        }
        public DataGridView dgv;

        public Item GetRandomItem()
        {
            var random = new Random();
            var item = new Item("Untitled", 0.0);
            switch (random.Next(3))
            {
                case 0:
                    item = new Item("eggs", 1.42);
                    break;
                case 1:
                    item = new Item("bread", 2.42);
                    break;
                case 2:
                    item = new Item("icesave", 640e6);
                    break;
            }
            return item;
        }

        public DemoForm()
        {
            this.InitializeComponent();

            this.Width = 800;
            this.Height = 600;

            var flp = new FlowLayoutPanel();
            flp.Dock = DockStyle.Fill;
            this.Controls.Add(flp);
            
            this.dgv = new DataGridView();
            dgv.Width = 300;
            dgv.Height = 300;

            // Köllum á delegate þegar breytt er vali á listview
            this.dgv.SelectionChanged += (e, o) => { 
                // Finnum núverandi item
                Item item = new Item();
                try {
                    item = ((Item[])this.dgv.DataSource)[dgv.CurrentRow.Index];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    // Hérna setjum við delegate í gang (s.b.r B)
                    this.ShoppingItemClicked(item);
                }
            };

            flp.Controls.Add(dgv);

            var btnSize = new Size(50, 25);

            var btnAdd = new Button();
            btnAdd.Size = btnSize;
            btnAdd.Text = "Add something";
            flp.Controls.Add(btnAdd);
            btnAdd.Click += delegate
                                {
                                    // Bætum við nýjum upplýsingum í listann okkar af söluvörum
                                    this.ShoppingList.AddItem(GetRandomItem());
                                    // Neyðum dgv til að birta ný gögn!
                                    this.dgv.DataSource = this.ShoppingList.Items;
                                };
            
            var btnSave = new Button();
            btnSave.Size = btnSize;
            btnSave.Text = "Save changes";
            flp.Controls.Add(btnSave);
            btnSave.Click += delegate
                                 {
                                     // ? Hvað gerist við form closed?
                                 };

            var btnDelete = new Button();
            btnDelete.Size = btnSize;
            btnDelete.Text = "Delete last item";
            flp.Controls.Add(btnDelete);
            btnDelete.Click += delegate
                                   {
                                       this.shoppingList.RemoveItem(this.shoppingList.Items[shoppingList.Items.Length - 1]);
                                       this.dgv.DataSource = this.ShoppingList.Items;
                                   };
        }
    }
}